/*
https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/solution/xing-cheng-liang-ge-yi-huo-xiang-deng-sh-jud0/
 */
import java.util.HashMap;
import java.util.Map;

public class Solution1442 {
    public int countTriplets(int[] arr) {
        int[] f=new int[arr.length+1];
        for (int i=0;i<arr.length;i++){
            f[i+1]=f[i]^arr[i];
        }
        Map<Integer,Integer> count=new HashMap<>();
        Map<Integer,Integer> sum=new HashMap<>();
        int ans=0;
        for (int i=0;i<arr.length;i++){
            if (count.containsKey(f[i+1])){
                ans=ans+count.get(f[i+1])*i-sum.get(f[i+1]);
            }
            count.put(f[i],count.getOrDefault(f[i],0)+1);
            sum.put(f[i],sum.getOrDefault(f[i],0)+i);
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1442().countTriplets(new int[]{2,3,1,6,7}));
    }
}
